********************************************************************************
* Fear and Favoritism in the Time of COVID-19
* Master Replication Script (Stata)
*
* This script replicates all tables in the main paper and appendix
*
* Authors: Han, Ho, Rhee, Tabakis
* Last updated: 2024
*
* Required packages: estout, ivreg2
* Install with: ssc install estout, replace
*               ssc install ivreg2, replace
********************************************************************************

clear all
set more off
cap log close

* ==============================================================================
* SETUP - MODIFY THIS PATH
* ==============================================================================

* Set working directory to the replication package folder
* Users should modify this path to match their local setup
global root "/Users/irhee/Dropbox/_doc/1_working/2_research/COVID_Replication/replication_package"

cd "$root"

* Create log file
log using "output/replication_log.txt", replace text

* Create output directories
cap mkdir "output"
cap mkdir "output/tables"
cap mkdir "output/figures"

di "Working directory: $root"
di "Date: `c(current_date)'"

* ==============================================================================
* LOAD AND MERGE DATA
* ==============================================================================

di _n "=== Loading Data ==="

* Load main survey data
import delimited "data/survey_data.csv", encoding(UTF-8) clear

* Merge GPS measures
merge 1:1 id using "data/gps_measures.dta", nogen
merge 1:1 id using "data/time_preferences.dta", nogen
merge 1:1 id using "data/media_counts.dta", nogen
merge 1:1 id using "data/neighbor_preferences.dta", nogen

di "Data loaded. N = " _N

* ==============================================================================
* VARIABLE CREATION
* ==============================================================================

di _n "=== Creating Variables ==="

qui {
	*---------------------------------------------------------------------------
	* Outcome Variables
	*---------------------------------------------------------------------------

	* Donation variables (already numeric)
	* q77_n2 = Outgroup donation (Korea Support Center for Foreign Workers)
	* q77_n3 = Ingroup donation (Korean Red Cross)
	* q77_n4 = Total donation

	* In-group minus out-group difference
	gen out_in = q77_n2 - q77_n3

	* Hyperlink click (behavioral measure)
	gen q110_ = 1 if q110 == "예"
	replace q110_ = 0 if q110 == "아니오"

	*---------------------------------------------------------------------------
	* Treatment Variable
	*---------------------------------------------------------------------------

	gen treat = 1 if (gubun == "Group. Fear_1 : Q74 -> Q76 -> Q77" | ///
	                  gubun == "Group. Fear_2 : Q76 -> Q74 -> Q77")
	replace treat = 0 if treat == .

	label define treat_lbl 0 "Happiness Treatment" 1 "Fear Treatment"
	label values treat treat_lbl

	*---------------------------------------------------------------------------
	* Instrumental Variable (Reported Fear)
	*---------------------------------------------------------------------------

	gen iv = 4 if q81_0 == "많이 느낀다"
	replace iv = 3 if q81_0 == "조금 느낀다"
	replace iv = 2 if q81_0 == "아주 조금 느낀다"
	replace iv = 1 if q81_0 == "전혀 느끼지 않는다"

	label var iv "Reported Fear (1-4 scale)"

	*---------------------------------------------------------------------------
	* Emotion Variables (for manipulation check)
	*---------------------------------------------------------------------------

	foreach x in q81_0 q81_0_n2 q81_0_n3 q81_0_n4 q81_0_n5 q81_0_n6 {
		gen `x'_ = 4 if `x' == "많이 느낀다"
		replace `x'_ = 3 if `x' == "조금 느낀다"
		replace `x'_ = 2 if `x' == "아주 조금 느낀다"
		replace `x'_ = 1 if `x' == "전혀 느끼지 않는다"
	}

	label var q81_0_ "Fear"
	label var q81_0_n2_ "Anger"
	label var q81_0_n3_ "Happiness"
	label var q81_0_n4_ "Sadness"
	label var q81_0_n5_ "Disgust"
	label var q81_0_n6_ "Surprise"

	*---------------------------------------------------------------------------
	* Demographic Variables
	*---------------------------------------------------------------------------

	* Sex (1=Male, 0=Female)
	gen sex = 1 if sq1 == "남성"
	replace sex = 0 if sq1 == "여성"
	label var sex "Sex (1=Male)"

	* Age
	rename sq2 age
	label var age "Age"

	* Education
	gen edu = 1 if sq4 == "중학교 졸업 이하"
	replace edu = 2 if sq4 == "고등학교 졸업"
	replace edu = 3 if sq4 == "대학 재학 중"
	replace edu = 4 if sq4 == "대학 졸업"
	replace edu = 5 if sq4 == "대학원 재학 중"
	replace edu = 6 if sq4 == "대학원 졸업"

	label define edu_lbl 1 "Up to middle school" 2 "High school" ///
	                     3 "College (enrolled)" 4 "College (graduated)" ///
	                     5 "Grad school (enrolled)" 6 "Grad school (graduated)"
	label values edu edu_lbl
	label var edu "Education"

	* Religion
	gen rel = 1 if q68 == "개신교"
	replace rel = 2 if q68 == "불교"
	replace rel = 3 if q68 == "천주교"
	replace rel = 4 if q68 == "종교 없음"
	replace rel = 5 if q68 == "기타"

	label define rel_lbl 1 "Christianity" 2 "Buddhism" 3 "Catholicism" ///
	                     4 "No religion" 5 "Other"
	label values rel rel_lbl
	label var rel "Religion"

	* Political ideology (1=Very liberal to 5=Very conservative)
	gen ideology = 1 if q66 == "매우 진보적"
	replace ideology = 2 if q66 == "다소 진보적"
	replace ideology = 3 if q66 == "중도"
	replace ideology = 4 if q66 == "다소 보수적"
	replace ideology = 5 if q66 == "매우 보수적"

	label var ideology "Political Ideology (1=Liberal, 5=Conservative)"

	* Ruling party support
	replace q107 = ustrtrim(q107)
	gen vote_inc = 1 if q107 == "예" & q108 == "더불어민주당"
	replace vote_inc = 0 if vote_inc == .
	label var vote_inc "Ruling Party Support"

	* Media consumption
	gen news_consumption = 1 if q102 == "전혀"
	replace news_consumption = 2 if q102 == "별로"
	replace news_consumption = 3 if q102 == "어느 정도"
	replace news_consumption = 4 if q102 == "매우 많이"
	label var news_consumption "News Consumption (1-4)"

	gen sns_frequency = 1 if q104 == "전혀"
	replace sns_frequency = 2 if q104 == "가끔"
	replace sns_frequency = 3 if q104 == "종종"
	replace sns_frequency = 4 if q104 == "매우 자주"
	label var sns_frequency "SNS Frequency (1-4)"

	*---------------------------------------------------------------------------
	* KGSS Outcome Variables (want groups to decrease)
	*---------------------------------------------------------------------------

	foreach x in q82 q82_n2 q82_n3 q82_n4 q82_n5 q82_n6 {
		gen `x'_ = 1 if `x' == "매우 증가하기를 바람"
		replace `x'_ = 2 if `x' == "다소 증가하기를 바람"
		replace `x'_ = 3 if `x' == "변화 없기를 바람"
		replace `x'_ = 4 if `x' == "다소 감소하기를 바람"
		replace `x'_ = 5 if `x' == "매우 감소하기를 바람"
		replace `x'_ = . if `x' == "모르겠다"
	}

	* Encode factor variables
	encode sq3, generate(sq3_)
	encode sq4, generate(sq4_)
	encode q68, generate(q68_)
	encode q103, generate(q103_)
}

di "Variables created successfully."

* ==============================================================================
* DEFINE CONTROL VARIABLE SETS
* ==============================================================================

* GPS preference controls
global gps gps_time gps_risk gps_alt gps_pr gps_nr gps_trust

* Demographic controls
global demo sex age i.sq3_ i.sq4_ i.q68_

* Political controls
global poli ideology vote_inc

* Media controls
global press news_consumption i.q103_ sns_frequency q105_count q106_count

* Control sets
global control1
global control2 $gps
global control3 $gps $demo
global control4 $gps $demo $poli
global control5 $gps $demo $poli $press

* ==============================================================================
* TABLE 1: SUMMARY AND BALANCE CHECK
* ==============================================================================

di _n "=== Table 1: Summary and Balance Check ==="

* Summary statistics by treatment group
tabstat age sex ideology vote_inc q77 q77_n3 q77_n2 q77_n4, ///
	by(treat) stat(mean sd n) format(%9.2f)

* Balance tests
foreach v in age sex ideology vote_inc {
	di _n "Balance test for `v':"
	reg `v' treat, robust
}

* Chi-square tests for categorical variables
tab edu treat, chi2
tab rel treat, chi2

* ==============================================================================
* TABLE 2: EMOTIONS SUMMARY AND MANIPULATION CHECK
* ==============================================================================

di _n "=== Table 2: Emotions Summary and Manipulation Check ==="

* Summary statistics for emotion variables
sum q81_0_ q81_0_n2_ q81_0_n3_ q81_0_n4_ q81_0_n5_ q81_0_n6_

* Manipulation check regressions
di _n "Manipulation Check - Effect of Treatment on Emotions:"
foreach v in q81_0_ q81_0_n2_ q81_0_n3_ q81_0_n4_ q81_0_n5_ q81_0_n6_ {
	di _n "Outcome: `v'"
	reg `v' treat, robust
}

* ==============================================================================
* TABLE 3: MAIN RESULTS - EFFECTS OF TREATMENT ON DONATIONS
* ==============================================================================

di _n "=== Table 3: Main Results ==="

* Panel A: Ingroup Donation (q77_n3)
di _n "PANEL A: INGROUP DONATION"
est clear

forval i = 1/5 {
	eststo treat`i': qui reg q77_n3 treat ${control`i'}, robust
	eststo report`i': qui reg q77_n3 iv ${control`i'}, robust
	eststo iv`i': qui ivreg2 q77_n3 (iv = treat) ${control`i'}, robust
}

di "Fear Treatment Effects:"
esttab treat*, keep(treat) star(* 0.1 ** 0.05 *** 0.01) se r2 nomtitle

di "Reported Fear Effects:"
esttab report*, keep(iv) star(* 0.1 ** 0.05 *** 0.01) se r2 nomtitle

di "IV Estimates:"
esttab iv*, keep(iv) star(* 0.1 ** 0.05 *** 0.01) se nomtitle

* Panel B: Outgroup Donation (q77_n2)
di _n "PANEL B: OUTGROUP DONATION"
est clear

forval i = 1/5 {
	eststo treat`i': qui reg q77_n2 treat ${control`i'}, robust
	eststo report`i': qui reg q77_n2 iv ${control`i'}, robust
	eststo iv`i': qui ivreg2 q77_n2 (iv = treat) ${control`i'}, robust
}

di "Fear Treatment Effects:"
esttab treat*, keep(treat) star(* 0.1 ** 0.05 *** 0.01) se r2 nomtitle

di "Reported Fear Effects:"
esttab report*, keep(iv) star(* 0.1 ** 0.05 *** 0.01) se r2 nomtitle

di "IV Estimates:"
esttab iv*, keep(iv) star(* 0.1 ** 0.05 *** 0.01) se nomtitle

* Panel C: Total Donation (q77_n4)
di _n "PANEL C: TOTAL DONATION"
est clear

forval i = 1/5 {
	eststo treat`i': qui reg q77_n4 treat ${control`i'}, robust
	eststo report`i': qui reg q77_n4 iv ${control`i'}, robust
	eststo iv`i': qui ivreg2 q77_n4 (iv = treat) ${control`i'}, robust
}

di "Fear Treatment Effects:"
esttab treat*, keep(treat) star(* 0.1 ** 0.05 *** 0.01) se r2 nomtitle

di "Reported Fear Effects:"
esttab report*, keep(iv) star(* 0.1 ** 0.05 *** 0.01) se r2 nomtitle

di "IV Estimates:"
esttab iv*, keep(iv) star(* 0.1 ** 0.05 *** 0.01) se nomtitle

* Panel D: Hyperlink Click (q110_)
di _n "PANEL D: HYPERLINK CLICK (SEEK FUNDRAISER INFO)"
est clear

forval i = 1/5 {
	eststo treat`i': qui reg q110_ treat ${control`i'}, robust
	eststo report`i': qui reg q110_ iv ${control`i'}, robust
	eststo iv`i': qui ivreg2 q110_ (iv = treat) ${control`i'}, robust
}

di "Fear Treatment Effects:"
esttab treat*, keep(treat) star(* 0.1 ** 0.05 *** 0.01) se r2 nomtitle

di "Reported Fear Effects:"
esttab report*, keep(iv) star(* 0.1 ** 0.05 *** 0.01) se r2 nomtitle

di "IV Estimates:"
esttab iv*, keep(iv) star(* 0.1 ** 0.05 *** 0.01) se nomtitle

* ==============================================================================
* TABLE 4: GPS VARIABLES AS OUTCOMES
* ==============================================================================

di _n "=== Table 4: GPS Variables as Outcomes ==="

foreach gps_var in gps_alt gps_pr gps_nr gps_trust {
	di _n "Outcome: `gps_var'"

	* No controls
	reg `gps_var' treat, robust

	* Full controls (excluding GPS)
	reg `gps_var' treat $demo $poli $press, robust
}

* ==============================================================================
* APPENDIX TABLES
* ==============================================================================

di _n "=== Appendix Tables ==="

* --- Table A1: First 4,000 Observations ---
di _n "TABLE A1: MAIN RESULTS WITH FIRST 4,000 OBSERVATIONS"

preserve
keep if _n <= 4000

est clear
forval i = 1/5 {
	eststo treat`i': qui reg q77_n2 treat ${control`i'}, robust
}

esttab treat*, keep(treat) star(* 0.1 ** 0.05 *** 0.01) se r2 nomtitle
restore

* --- Table A4: Political Interactions ---
di _n "TABLE A4: POLITICAL INTERACTIONS"

foreach y in q77_n2 q110_ {
	di _n "Outcome: `y'"

	* Ideology interaction
	di "Ideology X Treatment:"
	reg `y' treat##c.ideology, robust
	reg `y' treat##c.ideology $gps $demo $poli $press, robust

	* Ruling party interaction
	di "Ruling Party Support X Treatment:"
	reg `y' treat##vote_inc, robust
	reg `y' treat##vote_inc $gps $demo $poli $press, robust
}

* --- Table A5: Media Interactions ---
di _n "TABLE A5: MEDIA INTERACTIONS"

foreach y in q77_n2 q110_ {
	di _n "Outcome: `y'"

	foreach media_var in news_consumption q105_count q106_count {
		di "Media Variable: `media_var' X Treatment:"
		reg `y' treat##c.`media_var', robust
		reg `y' treat##c.`media_var' $gps $demo $poli $press, robust
	}
}

* --- Table A6: KGSS Outcomes ---
di _n "TABLE A6: KGSS OUTCOMES"
di "Question: Do you want the following groups to decrease in Korea?"

est clear
local i = 1
foreach y in q82_ q82_n2_ q82_n3_ q82_n4_ q82_n5_ q82_n6_ {
	eststo treat`i': qui reg `y' treat, robust
	eststo iv`i': qui ivreg2 `y' (iv = treat), robust
	local ++i
}

di "Treatment Effects:"
esttab treat*, keep(treat) star(* 0.1 ** 0.05 *** 0.01) se r2 nomtitle

di "IV Estimates:"
esttab iv*, keep(iv) star(* 0.1 ** 0.05 *** 0.01) se nomtitle

* --- Table A7: WVS Outcomes ---
di _n "TABLE A7: WVS OUTCOMES"
di "Question: Mention any that you would not like to have as neighbors"

est clear
local i = 1
foreach y in q94_1 q94_2 q94_3 q94_4 q94_5 q94_6 q94_7 {
	cap eststo treat`i': qui reg `y' treat, robust
	cap eststo iv`i': qui ivreg2 `y' (iv = treat), robust
	local ++i
}

di "Treatment Effects:"
esttab treat*, keep(treat) star(* 0.1 ** 0.05 *** 0.01) se r2 nomtitle

di "IV Estimates:"
esttab iv*, keep(iv) star(* 0.1 ** 0.05 *** 0.01) se nomtitle

* --- Table A9: T-Test Results ---
di _n "TABLE A9: T-TEST RESULTS FOR GPS VARIABLES"

foreach gps_var in gps_alt gps_pr gps_nr gps_trust gps_time gps_risk {
	di _n "Variable: `gps_var'"
	ttest `gps_var', by(treat)
}

* ==============================================================================
* EXPORT MAIN RESULTS TO LaTeX
* ==============================================================================

di _n "=== Exporting Tables to LaTeX ==="

* Table 3 - Main Results
est clear

* Panel B (Outgroup donation) with all specifications
forval i = 1/5 {
	eststo treat`i': qui reg q77_n2 treat ${control`i'}, robust
}

esttab treat* using "output/tables/table3_main_results.tex", ///
	replace booktabs ///
	keep(treat) ///
	star(* 0.1 ** 0.05 *** 0.01) ///
	se r2 ///
	mtitles("(1)" "(2)" "(3)" "(4)" "(5)") ///
	title("Effects of Fear Treatment on Outgroup Donations") ///
	addnotes("Standard errors in parentheses" "*** p<0.01, ** p<0.05, * p<0.1")

* ==============================================================================
* COMPLETION
* ==============================================================================

di _n "================================================================================"
di "REPLICATION COMPLETE"
di "================================================================================"
di _n "Tables have been exported to the output/tables/ directory."
di "Log file saved to output/replication_log.txt"

log close
